/*
 * @Author: ischen.x ischen.x@foxmail.com
 * @Date: 2024-07-11 13:31:52
 * @LastEditors: ischen.x ischen.x@foxmail.com
 * @LastEditTime: 2024-07-30 16:40:11
 * 
 * Copyright (c) 2024 by fhchengz, All Rights Reserved. 
 */
#include "LM75.h"

#define LM75_I2C_ADDRESS	(0x48<<1)

/**
 * @description: I2C write interface
 * @param dev_addr device address
 * @param reg_addr register address
 * @param *data write data
 * @param length length of data
 * @return 0: success
 */
static int (*lm75_i2c_mem_write)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);

/**
 * @description: I2C read interface
 * @param dev_addr device address
 * @param reg_addr register address
 * @param *data read data
 * @param length length of data
 * @return 0: success
 */
static int (*lm75_i2c_mem_read)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint8_t length);

int lm75_set_read_write_function(int(*read)(uint8_t,  uint8_t,  uint8_t *, uint8_t), int(*write)(uint8_t,  uint8_t,  uint8_t *, uint8_t))
{
    if (read && write) {
        lm75_i2c_mem_read = read;
        lm75_i2c_mem_write = write;
        return 0;
    } else {
        return -1;
    }
}

int lm75_get_temp(float *temp)
{
	int ret = 0;
	uint16_t readbuff = 0;
	uint8_t buff[2] = {0};
	if (lm75_i2c_mem_read(LM75_I2C_ADDRESS, 0x00, buff, 2)){
		ret = -1;
	} else {
		readbuff = (buff[0] << 8) | buff[1];
		*temp = (readbuff >> 5) * 0.125;
		ret = 0;
	}
	return ret;
}
